package com.example.listview;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;

public class EditActivity extends AppCompatActivity {
    private EditText edt_Name;
    private EditText edt_Age;
    private RadioButton radio_Button1;
    private RadioButton radio_Button2;
    private RadioButton radio_Button3;
    private RadioButton radio_Button4;
    private RadioButton radio_Button5;
    private Button btnSave;
    private String no;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit);

        setTitle("编辑学生");

        findViews();

        initViewData();

        setBtnClickListener();

    }

    /**
     * 绑定各个界面组件
     */
    private void findViews() {
        edt_Name = findViewById(R.id.edtName);
        edt_Age = findViewById(R.id.edtAge);

        radio_Button1 = findViewById(R.id.radioBtn1);
        radio_Button2 = findViewById(R.id.radioBtn2);
        radio_Button3 = findViewById(R.id.radioBtn3);
        radio_Button4 = findViewById(R.id.radioBtn4);
        radio_Button5 = findViewById(R.id.radioBtn5);

    }

    /**
     *初始化界面数据
     */
    private void initViewData() {
        Intent intent = getIntent();
        String name = intent.getStringExtra("Name");
        int age = intent.getIntExtra("Age", 0);
        final int imgId = intent.getIntExtra("ImgId", R.drawable.dog1);
        no = intent.getStringExtra("NO");

        edt_Name.setText(name);
        edt_Age.setText(String.valueOf(age));

        switch (imgId) {
            case R.drawable.dog1:
                radio_Button1.setChecked(true);
                break;

            case R.drawable.dog2:
                radio_Button2.setChecked(true);
                break;

            case R.drawable.dog3:
                radio_Button3.setChecked(true);
                break;

            case R.drawable.dog4:
                radio_Button1.setChecked(true);
                break;

            case R.drawable.dog5:
                radio_Button5.setChecked(true);
                break;

        }
    }
    /**
     *保存按钮的点击事件
     */
    private void setBtnClickListener() {
        btnSave = findViewById(R.id.btn_Save);
        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = edt_Name.getText().toString();
                int age = Integer.parseInt(edt_Age.getText().toString());
                int ImgId = R.drawable.dog1;

                if (radio_Button1.isChecked()) {
                    ImgId = R.drawable.dog1;
                } else if (radio_Button2.isChecked()) {
                    ImgId = R.drawable.dog2;
                } else if (radio_Button3.isChecked()) {
                    ImgId = R.drawable.dog3;
                } else if (radio_Button4.isChecked()) {
                    ImgId = R.drawable.dog4;
                } else if (radio_Button5.isChecked()) {
                    ImgId = R.drawable.dog5;
                }

                Intent intent = new Intent();

                intent.putExtra("Name", name);
                intent.putExtra("Age", age);
                intent.putExtra("Img", ImgId);
                intent.putExtra("NO", no);

                setResult(3001, intent);
                finish();
            }
        });
    }
}
